home *** CD-ROM | disk | FTP | other *** search
Text File | 1986-09-07 | 4.9 KB | 166 lines | [TEXT/MACA] |
- /*
- * draw.c - miscellaneous drawing routines for Tablut.
- *
- */
-
- #include <quickdraw.h>
- #include <window.h>
- #include <resource.h>
- #include <toolutil.h>
- #include <packages.h>
- #include <control.h>
- #include <memory.h>
- #include "tablut.h"
-
- #define LIGHTPAT 129 /* Resource ID of the highlight pattern */
-
- #define STATSTRS 129 /* Resource ID of the game status strings */
- #define SS_MOVE 1 /* index for the "Move:" string */
- #define SS_WHITE 2 /* index for the "White's Game" string */
- #define SS_BLACK 3 /* index for the "Black's Game" string */
-
- Rect upboardrect, lwboardrect; /* destination rects for the board */
- PicHandle upboard, lwboard; /* upper and lower board parts */
-
- char movestr[65]; /* Pascal "Move:" string */
- char whitestr[65]; /* Pascal "White's Game" string */
- char blackstr[65]; /* Pascal "Black's Game" string */
-
- /*
- * getboard() - get the pieces of the board picture together
- * in the appropriate part of the window
- */
- getboard()
- {
- Point offboard; /* offset from board coords to window coords */
- /*
- * get handles for the pictures of the board,
- * then create the zero-aligned rectangles for the parts.
- * The lower board goes directly below the upper board.
- */
-
- upboard = (PicHandle) GetNamedResource((long) 'PICT', "\Pupper_board");
- lwboard = (PicHandle) GetNamedResource((long) 'PICT', "\Plower_board");
- HLock((Handle) upboard);
- HLock((Handle) lwboard);
- movmem((char *) &((*upboard)->picFrame), (char *) &upboardrect,
- sizeof(Rect));
- movmem((char *) &((*lwboard)->picFrame), (char *) &lwboardrect,
- sizeof(Rect));
- OffsetRect(&upboardrect,
- -((*upboard)->picFrame.left), -((*upboard)->picFrame.top));
- OffsetRect(&lwboardrect, -((*lwboard)->picFrame.left),
- -((*lwboard)->picFrame.top) + upboardrect.bottom);
- HUnlock((Handle) upboard);
- HUnlock((Handle) lwboard);
-
- offboard.h = (mywindow->portRect.left + mywindow->portRect.right) / 2 -
- (upboardrect.left + upboardrect.right) / 2;
- offboard.v = (mywindow->portRect.top + mywindow->portRect.bottom) / 2 -
- (upboardrect.top + lwboardrect.bottom) / 2;
- OffsetRect(&upboardrect, offboard.h, offboard.v);
- OffsetRect(&lwboardrect, offboard.h, offboard.v);
-
- SetRect(&boardrect, upboardrect.left, upboardrect.top,
- lwboardrect.right, lwboardrect.bottom);
- boardcenter.h = (boardrect.left + boardrect.right) / 2;
- boardcenter.v = (boardrect.top + boardrect.bottom) / 2;
- }
-
- /*
- * getstate() - load the game status report resources
- */
- getstate()
- {
- GetIndString(movestr, STATSTRS, SS_MOVE);
- GetIndString(whitestr, STATSTRS, SS_WHITE);
- GetIndString(blackstr, STATSTRS, SS_BLACK);
- }
-
- /*
- * drawwindow() - draw the tablut window,
- * drawing the board and pieces in the appropriate places.
- * This routine is called to satisfy update events, so it has to save &
- * restore the GraphPort (in case we update when this isn't
- * the front window).
- */
- drawwindow()
- {
- GrafPtr saveport;
- struct pieceimage *p; /* the piece being examined */
-
- BeginUpdate(mywindow);
- GetPort(&saveport);
- SetPort(mywindow);
- DrawPicture(upboard, &upboardrect);
- DrawPicture(lwboard, &lwboardrect);
- for (p = &piece[0]; p < &piece[NUMPIECES]; ++p) {
- if (onscreen(p)) {
- splatpiece((int) (p - &piece[0]));
- }
- }
- DrawControls(mywindow);
- drawstate();
- SetPort(saveport);
- EndUpdate(mywindow);
- }
-
- /*
- * drawstate() - draw the move number and winner (if any)
- */
- drawstate()
- {
- char numstr[20]; /* (C) Ascii move number */
- short len; /* # of chars in numstr[] */
- short wid, wid2; /* screen width of a string */
- FontInfo f; /* info about the screen font */
- Rect r; /* a rectangle to erase */
-
- MoveTo(16, 100); DrawString(movestr);
- NumToString((long) (curmove - 1), numstr);
- ptoc(numstr);
- len = strlen(numstr);
- wid = TextWidth(numstr, 0, len);
- /*
- * "why not just use srcCopy for the text?" I hear the reader saying.
- * "Because", I reply, "srcCopy for text erases far too many pixels."
- * See the description of TextMode() in Inside Macintosh.
- */
- GetFontInfo(&f);
- SetRect(&r, 56, 100 - f.ascent, 96, 100 + f.descent);
- EraseRect(&r);
- MoveTo(96 - wid, 100); DrawText(numstr, 0, len);
-
- wid = StringWidth(whitestr);
- wid2 = StringWidth(blackstr);
- if (wid < wid2) wid = wid2;
- SetRect(&r, 16, 120 - f.ascent, 16 + wid, 120 + f.descent);
- EraseRect(&r);
- MoveTo(16, 120);
- if (winner == WHITEWIN) {
- DrawString(whitestr);
- } else if (winner == BLACKWIN) {
- DrawString(blackstr);
- }
- }
-
- /*
- * lightrect() - highlight (or unhighlight) the given rectangle
- * by Xor'ing in a light pattern.
- */
- lightrect(rp)
- Rect *rp;
- {
- PenState savepen;
- PatHandle blinkpat;
-
- GetPenState(&savepen);
- PenMode(patXor);
- blinkpat = GetPattern(LIGHTPAT);
- HLock((Handle) blinkpat);
- PenPat(*blinkpat);
- PaintRect(rp);
- HUnlock((Handle) blinkpat);
- SetPenState(&savepen);
- }
-